package com.mszlu.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mszlu.blog.common.Result;
import com.mszlu.blog.entity.SysUser;
import com.mszlu.blog.service.LoginService;
import com.mszlu.blog.service.SysUserServive;
import com.mszlu.blog.utils.JWTUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    //  20001       图片上传失败
    //  0       服务器错误



    private static final String salt = "mszlu!@#";

    @Autowired
    private SysUserServive sysUserServive;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Result login(SysUser logUser) {

        String account = logUser.getAccount();
        String password = DigestUtils.md5DigestAsHex( ( logUser.getPassword()+ salt ).getBytes() );

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)){
            return Result.pwdError();
        }
        SysUser user = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getAccount,account).eq(SysUser::getPassword,password));
        if (user == null ){
            return Result.pwdError();
        }
        String token = JWTUtils.createToken(user.getId());

        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(user),3, TimeUnit.HOURS);

        return Result.success(token);
    }

    @Override
    public Result register(SysUser user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String nickname = user.getNickname();

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password) || StringUtils.isBlank(nickname) ) {
            return Result.registerError();
        }
        SysUser regUser = sysUserServive.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getAccount,account));
        if (regUser != null) {
            return Result.registerError("账号已存在");
        }

        //开始插入新用户
        regUser = new SysUser();
        regUser.setNickname(nickname);
        regUser.setAccount(account);
        regUser.setPassword(DigestUtils.md5DigestAsHex( (password + salt).getBytes() ));
        regUser.setCreateDate(System.currentTimeMillis());
        regUser.setAvatar("/static/img/logo.b3a48c0.png");
        regUser.setAdmin(true);
        regUser.setDeleted(false);
        regUser.setSalt("");
        regUser.setStatus("");
        regUser.setEmail("");

        boolean save = sysUserServive.save(regUser);

        if (save == true) {
            return Result.success("注册成功");
        }

        return Result.error("网速太慢了");
    }

    @Override
    public SysUser checkLogin(String token){
        if (StringUtils.isBlank(token)){
            return null;
        }

        Map<String, Object> map = JWTUtils.checkToken(token);
        if (map == null){
            return null;
        }

        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);

        if (userJson == null){
            return null;
        }

        SysUser parse = JSON.parseObject(userJson,SysUser.class);
        return parse;
    }




}
